/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canvaswork;

import geomerative.RG;
import geomerative.RShape;
import java.lang.reflect.Method;

/**
 *
 * @author jonatanhilden
 */
public class Button implements Selectable {

    private Main main;
    int xpos, ypos;
    int width, height;
    String text;
    private Object actionObject;
    private Method actionMethod;
    RShape shp;
    RShape intersectShp;
    private boolean isToggled = false;

    Button(Main main, String buttontext, int x, int y, int w, int h) {
        this.main = main;

        xpos = x;
        ypos = y;
        width = w;
        height = h;
        text = buttontext;

        shp = RShape.createRectangle(xpos, ypos, width, height);

    }

    /**
     * Set the method that will be called when the button is pressed.
     *
     * @param obj        the instance
     * @param methodName the name of the method on the instance. The method should not take any arguments.
     */
    public void setActionMethod(Object obj, String methodName) {
        actionObject = obj;
        try {
            actionMethod = obj.getClass().getMethod(methodName);
        } catch (NullPointerException e) {
            throw new IllegalArgumentException("Ojbect or method name cannot be null.");
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException("Cannot find method " + methodName + " on object " + actionObject);
        }
    }

    void draw() {
        if (main.isOver(this)) {
            main.fill(main.color(main.hue(main.defaultColor), main.saturation(main.defaultColor), ((main.brightness(main.defaultColor)) / 2)));

        } else if (isToggled) {
            main.fill(main.defaultColor);

        } else if (main.isOver(this) && main.mousePressed) {
            main.noFill();
        }
        else{
            main.noFill();
        }

        RG.shape(shp);

        if (main.isOver(this) || isToggled) {
            main.fill(0);
        }
        main.text(text, xpos + 2, ypos + height - 2);



    }

    public RShape getShape() {
        return shp;
    }

    void toggle() {
        if (isToggled == false) {
            isToggled = true;
        } else {
            isToggled = false;
        }
    }

    boolean getIsPressed(){
    return isToggled;}


}
